#!/bin/bash

# 设置变量
dir="/home/node54_tmpdata/yzli/wenet_whisper_finetune/examples/wenetspeech/whisper/exp/qwen2_multi_task_2_6gpus_gxl_adapter_init_asr-sot_whisper"
dir="/home/node54_tmpdata/xlgeng/code/wenet_whisper_finetune/examples/wenetspeech/whisper/exp"
ckpt_name="epoch_6.pt"

test_data_dir="/home/node54_tmpdata/xlgeng/code/wenet_whisper_finetune_xlgeng/examples/wenetspeech/whisper/data/test_sets"
test_tasks="style chat mid_background pure_background end_background emotion align gender age caption"
#test_tasks='align age'
#output_dir=./res/stage_2_$ckpt_name
mkdir -p $output_dir


if [[ "$test_tasks" == *"chat"* ]]; then
  start_time=$(date +%s)
  # 设置 test_sets
  test_sets="chat"
  # 遍历 test_sets
  for test_set in $test_sets; do
    echo "test_set: $test_set"
    # 拼接路径
      hyp_text_path_item=$dir/test_${ckpt_name}/${test_set}/llmasr_decode/text
      ref_text_path_item="$test_data_dir/${test_set}/text"
      res_path_item=$dir/test_${ckpt_name}/${test_set}/llmasr_decode/wer_gxl

      # 检查文件是否存在
      if [ ! -f "$hyp_text_path_item" ]; then
        echo "text path $hyp_text_path_item does not exist."
        continue
      fi

      # 执行 Python 脚本
      python compute_bleu_for_chat_task.py --hyp_file "$hyp_text_path_item" --ref_file "$ref_text_path_item" --output_file "$res_path_item"
  done
  end_time=$(date +%s)
  elapsed_time=$((end_time - start_time))
  echo "执行时间：$elapsed_time 秒"
fi



if [[ "$test_tasks" == *"age"* ]]; then
  start_time=$(date +%s)
  # 设置 test_sets
  test_sets="age/split_8/index_0 age/split_8/index_1 age/split_8/index_2 age/split_8/index_3 age/split_8/index_4 age/split_8/index_5 age/split_8/index_6 age/split_8/index_7 age/child"
  test_sets='age age/child'
  # 遍历 test_sets
  for test_set in $test_sets; do
    echo "test_set: $test_set"

    # 拼接路径
    hyp_text_path_item="$dir/test_${ckpt_name}/${test_set}/llmasr_decode/text"
    ref_text_path_item="$test_data_dir/${test_set}/text"
    output_dir_tmp=$dir/test_${ckpt_name}/${test_set}/llmasr_decode
    mkdir -p $output_dir_tmp

    # 检查文件是否存在
    if [ ! -f "$hyp_text_path_item" ]; then
      echo "text path $hyp_text_path_item does not exist."
      continue
    fi

    # 执行 Python 脚本
    python gxl_compute_acc_for_age.py --hyp_file "$hyp_text_path_item" --ref_file "$ref_text_path_item" --output_dir "$output_dir_tmp"
  done
  end_time=$(date +%s)
  elapsed_time=$((end_time - start_time))
  echo "执行时间：$elapsed_time 秒"
fi



if [[ "$test_tasks" == *"gender"* ]]; then
  start_time=$(date +%s)
  # 设置 test_sets
  test_sets="gender"

  # 遍历 test_sets
  for test_set in $test_sets; do
    echo "test_set: $test_set"

    # 拼接路径
    hyp_text_path_item="$dir/test_${ckpt_name}/${test_set}/llmasr_decode/text"
    ref_text_path_item="$test_data_dir/${test_set}/text"
    output_dir_tmp=$dir/test_${ckpt_name}/${test_set}/llmasr_decode
    mkdir -p $output_dir_tmp

    # 检查文件是否存在
    if [ ! -f "$hyp_text_path_item" ]; then
      echo "text path $hyp_text_path_item does not exist."
      continue
    fi

    # 执行 Python 脚本
    python gxl_compute_acc_for_gender.py --hyp_file "$hyp_text_path_item" --ref_file "$ref_text_path_item" --output_dir "$output_dir_tmp"
  done
  end_time=$(date +%s)
  elapsed_time=$((end_time - start_time))
  echo "执行时间：$elapsed_time 秒"
fi




if [[ "$test_tasks" == *"caption"* ]]; then
  start_time=$(date +%s)
  # 设置 test_sets
  test_sets="caption/caption_1 caption/caption_2"

  # 遍历 test_sets
  for test_set in $test_sets; do
    echo "test_set: $test_set"

    # 拼接路径
    hyp_text_path_item="$dir/test_${ckpt_name}/${test_set}/llmasr_decode/text"
    ref_text_path_item="$test_data_dir/${test_set}/text"
    output_dir_tmp=$dir/test_${ckpt_name}/${test_set}/llmasr_decode
    mkdir -p $output_dir_tmp

    # 检查文件是否存在
    if [ ! -f "$hyp_text_path_item" ]; then
      echo "text path $hyp_text_path_item does not exist."
      continue
    fi

    # 执行 Python 脚本
    python gxl_compute_acc_for_caption.py --hyp_file "$hyp_text_path_item" --ref_file "$ref_text_path_item" --output_dir "$output_dir_tmp"
  done
  end_time=$(date +%s)
  elapsed_time=$((end_time - start_time))
  echo "执行时间：$elapsed_time 秒"
fi



if [[ "$test_tasks" == *"emotion"* ]]; then
  start_time=$(date +%s)
  # 设置 test_sets
  test_sets="emotion"

  # 遍历 test_sets
  for test_set in $test_sets; do
    echo "test_set: $test_set"

    # 拼接路径
    hyp_text_path_item="$dir/test_${ckpt_name}/${test_set}/llmasr_decode/text"
    ref_text_path_item="$test_data_dir/${test_set}/text"
    output_dir_tmp=$dir/test_${ckpt_name}/${test_set}/llmasr_decode
    mkdir -p $output_dir_tmp

    # 检查文件是否存在
    if [ ! -f "$hyp_text_path_item" ]; then
      echo "text path $hyp_text_path_item does not exist."
      continue
    fi

    # 执行 Python 脚本
    python gxl_compute_acc_for_emotion.py --hyp_file "$hyp_text_path_item" --ref_file "$ref_text_path_item" --output_dir "$output_dir_tmp"
  done
  end_time=$(date +%s)
  elapsed_time=$((end_time - start_time))
  echo "执行时间：$elapsed_time 秒"
fi

if [[ "$test_tasks" == *"align"* ]]; then
  start_time=$(date +%s)
  # 设置 test_sets
  test_sets="align/aishell2/split_8/index_0 align/aishell2/split_8/index_1 align/aishell2/split_8/index_2 align/aishell2/split_8/index_3 align/aishell2/split_8/index_4 align/aishell2/split_8/index_5 align/aishell2/split_8/index_6 align/aishell2/split_8/index_7"
  test_sets='align/aishell2'

  # 遍历 test_sets
  for test_set in $test_sets; do
    echo "test_set: $test_set"

    # 拼接路径
    hyp_text_path_item="$dir/test_${ckpt_name}/${test_set}/llmasr_decode/text"
    ref_text_path_item="$test_data_dir/${test_set}/text"
    output_dir_tmp=$dir/test_${ckpt_name}/${test_set}/llmasr_decode
    mkdir -p $output_dir_tmp

    # 检查文件是否存在
    if [ ! -f "$hyp_text_path_item" ]; then
      echo "text path $hyp_text_path_item does not exist."
      continue
    fi

    # 执行 Python 脚本
    python gxl_compute_acc_for_align.py --hyp_file "$hyp_text_path_item" --ref_file "$ref_text_path_item" --output_dir "$output_dir_tmp"
  done
  end_time=$(date +%s)
  elapsed_time=$((end_time - start_time))
  echo "执行时间：$elapsed_time 秒"
fi


if [[ "$test_tasks" == *"end_background"* ]]; then
  start_time=$(date +%s)
  # 设置 test_sets
  test_sets="add_end_background"

  # 遍历 test_sets
  for test_set in $test_sets; do
    echo "test_set: $test_set"

    # 拼接路径
    hyp_text_path_item="$dir/test_${ckpt_name}/${test_set}/llmasr_decode/text"
    ref_text_path_item="$test_data_dir/${test_set}/text"
    output_dir_tmp=$dir/test_${ckpt_name}/${test_set}/llmasr_decode
    mkdir -p $output_dir_tmp

    # 检查文件是否存在
    if [ ! -f "$hyp_text_path_item" ]; then
      echo "text path $hyp_text_path_item does not exist."
      continue
    fi

    # 执行 Python 脚本
    python gxl_compute_acc_for_end_background.py --hyp_file "$hyp_text_path_item" --ref_file "$ref_text_path_item" --output_dir "$output_dir_tmp"
  done
  end_time=$(date +%s)
  elapsed_time=$((end_time - start_time))
  echo "执行时间：$elapsed_time 秒"
fi


if [[ "$test_tasks" == *"pure_background"* ]]; then
  start_time=$(date +%s)
  # 设置 test_sets
  test_sets="pure_background"

  # 遍历 test_sets
  for test_set in $test_sets; do
    echo "test_set: $test_set"

    # 拼接路径
    hyp_text_path_item="$dir/test_${ckpt_name}/${test_set}/llmasr_decode/text"
    ref_text_path_item="$test_data_dir/${test_set}/text"
    output_dir_tmp=$dir/test_${ckpt_name}/${test_set}/llmasr_decode
    mkdir -p $output_dir_tmp

    # 检查文件是否存在
    if [ ! -f "$hyp_text_path_item" ]; then
      echo "text path $hyp_text_path_item does not exist."
      continue
    fi

    # 执行 Python 脚本
    python gxl_compute_acc_for_pure_background.py --hyp_file "$hyp_text_path_item" --ref_file "$ref_text_path_item" --output_dir "$output_dir_tmp"
  done
  end_time=$(date +%s)
  elapsed_time=$((end_time - start_time))
  echo "执行时间：$elapsed_time 秒"
fi


if [[ "$test_tasks" == *"mid_background"* ]]; then
  start_time=$(date +%s)
  # 设置 test_sets
  test_sets="add_mid_background"

  # 遍历 test_sets
  for test_set in $test_sets; do
    echo "test_set: $test_set"

    # 拼接路径
    hyp_text_path_item="$dir/test_${ckpt_name}/${test_set}/llmasr_decode/text"
    ref_text_path_item="$test_data_dir/${test_set}/text"
    output_dir_tmp=$dir/test_${ckpt_name}/${test_set}/llmasr_decode
    mkdir -p $output_dir_tmp

    # 检查文件是否存在
    if [ ! -f "$hyp_text_path_item" ]; then
      echo "text path $hyp_text_path_item does not exist."
      continue
    fi

    # 执行 Python 脚本
    python gxl_compute_acc_for_mid_background.py --hyp_file "$hyp_text_path_item" --ref_file "$ref_text_path_item" --output_dir "$output_dir_tmp"
  done
  end_time=$(date +%s)
  elapsed_time=$((end_time - start_time))
  echo "执行时间：$elapsed_time 秒"
fi


if [[ "$test_tasks" == *"style"* ]]; then
  start_time=$(date +%s)
  # 设置 test_sets
  test_sets="style"

  # 遍历 test_sets
  for test_set in $test_sets; do
    echo "test_set: $test_set"

    # 拼接路径
    hyp_text_path_item="$dir/test_${ckpt_name}/${test_set}/llmasr_decode/text"
    ref_text_path_item="$test_data_dir/${test_set}/text"
    output_dir_tmp=$dir/test_${ckpt_name}/${test_set}/llmasr_decode
    mkdir -p $output_dir_tmp

    # 检查文件是否存在
    if [ ! -f "$hyp_text_path_item" ]; then
      echo "text path $hyp_text_path_item does not exist."
      continue
    fi

    # 执行 Python 脚本
    python gxl_compute_acc_for_style.py --hyp_file "$hyp_text_path_item" --ref_file "$ref_text_path_item" --output_dir "$output_dir_tmp"
  done
  end_time=$(date +%s)
  elapsed_time=$((end_time - start_time))
  echo "执行时间：$elapsed_time 秒"
fi